import ApiActivity from "@/api/activity";
import useRequest from "@/hooks/useRequest";
import { ProSelect } from "@/modules/package/form";
import { ElFormItem, ElMessageBox } from "element-plus";
import { h, ref } from "vue";

export default function useAduit(callback: any) {
  // 审核接口
  const {
    isFetching,
    run: runAudit,
    onFetchResponse: onResponseAduit,
  } = useRequest(ApiActivity.activityApplyAudit, { immediate: false });

  // 提交审核成功
  onResponseAduit(() => callback?.());

  /**
   *  审核事件
   * @param row 数据
   * @param audit_status 1通过 ｜ 2拒绝
   */
  const toAduit = async (row: any, audit_status: 1 | 2) => {
    let audit_reason;

    const stand_id = ref(row.stand_id);

    if (audit_status == 2) {
      const { value } = await ElMessageBox.prompt(
        "建议填写不通过缘由",
        "审核：不通过",
        {
          confirmButtonText: "提交",
          cancelButtonText: "取消",
          inputPlaceholder: "请填写",
        }
      );
      audit_reason = value;
    } else {
      // await ElMessageBox.alert("请确认操作？", "审核通过");



      const { data } = await useRequest(ApiActivity.standList, {
        defaultParams: { id: row.id, page: 1, size: 100 },
      });

      await ElMessageBox({
        title: "审核确认操作",
        message: () => {
          return h(
            ElFormItem,
            {
              label: "展位",
              style: "width: 396px",
            },
            [
              h(ProSelect, {
                valueKey: "id",
                labelKey: "stand_title",
                options: data.value.list,
                modelValue: stand_id.value,
                "onUpdate:modelValue": (e: number) => (stand_id.value = e),
              }),
            ]
          );
        },
      });
    }

    runAudit({
      id: row.id,
      audit_status,
      audit_reason,
      stand_id: stand_id.value
    });
  };

  return {
    isFetching,
    toAduit,
  };
}
